package com.xindaoapp.henghua.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xindaoapp.henghua.application.XinDaoApplication;
import com.xindaoapp.henghua.entry.PropertyGatherInfo;
import com.xindaoapp.henghua.entry.PropertyLabelInfo;
import com.xindaoapp.henghua.entry.ScanData;
import com.xindaoapp.henghua.entry.TagsInfo;

/**
 * 数据更新之后进行通知
 * 
 * @author chenghao
 * 
 */
public class UpdateDataChanged {
	private final List<OnUpdateDataChangedListener> listener = new ArrayList<OnUpdateDataChangedListener>();
	/** 全部总信息 */
	private List<PropertyGatherInfo> mPropertyGatherInfos;
	/** 全部总信息的映射 */
	private final Map<String, PropertyGatherInfo> mPropertyGatherInfoMap = new HashMap<String, PropertyGatherInfo>();

	private List<TagsInfo> mTagsInfos = new ArrayList<TagsInfo>();

	public void add(OnUpdateDataChangedListener l) {
		listener.add(l);
	}

	public void remove(OnUpdateDataChangedListener l) {
		listener.remove(l);
	}

	/**
	 * 通知注册监听的页面
	 */
	public void notifyDataChanged() {
		for (OnUpdateDataChangedListener l : listener) {
			l.onUpdateDataChanged();
		}
	}

	/**
	 * 刷新数据
	 */
	public void refurshDataChanged() {
		setPropertyGatherInfos(XinDaoApplication.get().getDBHelper().getPropertyGatherInfos());
		notifyDataChanged();
	}

	public List<PropertyGatherInfo> getPropertyGatherInfos() {
		return mPropertyGatherInfos;
	}

	public void setPropertyGatherInfos(List<PropertyGatherInfo> list) {
		mPropertyGatherInfos = list;
		for (PropertyGatherInfo info : list) {
			mPropertyGatherInfoMap.put(info.id, info);
		}
	}

	/**
	 * 添加标签
	 * @param info
	 */
	public void addPropertyLabelInfo(PropertyLabelInfo info) {
		XinDaoApplication.get().getUpdateDataChanged().getPropertyGatherInfo(info.assert_id).propertyLabelInfos
				.add(info);
	}

	/**
	 * 移除标签
	 * @param assertId
	 * @param id
	 */
	public void removePropertyLabelInfo(String assertId, String tagCode) {
		List<PropertyLabelInfo> list = mPropertyGatherInfoMap.get(assertId).propertyLabelInfos;
		for (PropertyLabelInfo info : list) {
			if (info.tag_code.equals(tagCode)) {
				list.remove(info);
				return;
			}
		}
	}

	public PropertyLabelInfo getPropertyLabelInfo(String assertId, String tagCode) {
		List<PropertyLabelInfo> list = mPropertyGatherInfoMap.get(assertId).propertyLabelInfos;
		for (PropertyLabelInfo info : list) {
			if (info.tag_code.equals(tagCode)) {
				return info;
			}
		}
		return null;
	}

	public ScanData getScanData(String assertId, String tagCode) {
		List<ScanData> list = mPropertyGatherInfoMap.get(assertId).scanDatas;
		for (ScanData info : list) {
			if (info.tag_code.equals(tagCode)) {
				return info;
			}
		}
		return null;
	}

	public PropertyGatherInfo getPropertyGatherInfo(String key) {
		return mPropertyGatherInfoMap.get(key);
	}

	public void addScanData(ScanData scanData) {
		XinDaoApplication.get().getUpdateDataChanged().getPropertyGatherInfo(scanData.assert_id).scanDatas
				.add(scanData);
	}

	public List<TagsInfo> getTagsInfos() {
		return mTagsInfos;
	}

	public void setTagsInfos(List<TagsInfo> tagsInfos) {
		this.mTagsInfos = tagsInfos;
	}

	/**
	 * 移除扫描记录
	 * @param assertId
	 * @param id
	 */
	public void removeScanData(String assertId, String tagCode) {
		List<ScanData> list = mPropertyGatherInfoMap.get(assertId).scanDatas;
		for (ScanData info : list) {
			if (info.tag_code.equals(tagCode)) {
				list.remove(info);
				return;
			}
		}
	}

	/**
	 * 判断标签是否存在
	 * @param tagCode
	 * @return
	 */
	public boolean isExistLabel(String tagCode) {
		for (TagsInfo info : mTagsInfos) {
			if (info.code.equals(tagCode)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 数据更新之后进行通知的接口
	 * 
	 * @author chenghao
	 * 
	 */
	public interface OnUpdateDataChangedListener {
		public void onUpdateDataChanged();
	}
}
